package com.geekgo.gges.web;

import com.geekgo.gges.model.Article;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Administrator on 2018/5/1.
 */
@RestController
public class ESController {

    private final Logger logger = LoggerFactory.getLogger(ESController.class);

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @PostMapping("initIndex")
    public Object initESIndex() {
        //创建索引
                    /*{
                "index": {
                "analysis": {
                    "analyzer": {
                        "ik_pinyin_analyzer": {
                            "type": "custom",
                                    "tokenizer": "ik_smart",
                                    "filter": ["my_pinyin", "word_delimiter"]
                        }
                    },
                    "filter": {
                        "my_pinyin": {
                            "type": "pinyin",
                                    "first_letter": "prefix",
                                    "padding_char": " "
                        }
                    }
                }
            }
            }*/
        String setting = "{\"index\" : "
                + "{\"analysis\" : "
                + "{\"analyzer\" : "
                + "{\"ik_pinyin_analyzer\" : "
                + "{\"tokenizer\" : \"ik_max_word\", "
                + "\"filter\" : [\"my_pinyin\",\"word_delimiter\"]}},"
                + "\"filter\" : {\"my_pinyin\" : "
                + "{\"type\" : \"pinyin\", \"first_letter\" : \"prefix\",\"padding_char\" : \" \" }}}}}";
        boolean boo = elasticsearchTemplate.indexExists(Article.class);
        if (boo) {
            boolean delBoo = elasticsearchTemplate.deleteIndex(Article.class);
            if (delBoo) {
                boolean createIndex = elasticsearchTemplate.createIndex(Article.class, setting);
                logger.info("blog索引创建成功？：{}", createIndex);
                //创建类型并设置mapping
                /*{
                    "article": {
                    "properties": {
                        "name": {
                            "type": "keyword",
                             "fields": {
                                "pinyin": {
                                    "type": "text",
                                            "store": "no",
                                            "term_vector": "with_positions_offsets",
                                            "analyzer": "ik_pinyin_analyzer",
                                            "boost": 10
                                }
                            }
                        }
                    }
                }
                }*/
                /*String mapping = "{\"" + "article" + "\": {\"properties\": "
                        + "{\"title\": {\"type\": \"keyword\",\"analyzer\": \"ik_pinyin_analyzer\"}}}}";*/
                String mapping2 = "{\"" + "article" + "\": {\"properties\": "
                        + "{\"title\": {\"type\": \"keyword\",\"fields\": {\"pinyin\": {\"type\": \"text\",\"store\": \"no\",\"term_vector\": \"with_positions_offsets\",\"analyzer\": \"ik_pinyin_analyzer\",\"boost\": 10}}}}}}";
                logger.info("Mapping:{}",mapping2);
                boolean booMapping = elasticsearchTemplate.putMapping("blog", "article", mapping2);
                logger.info("artile mapping 创建成功？：{}", booMapping);
            }
        }

        return "success";
    }
}
